package cac.fenjian.resource.ExcelExport.rest;

import cac.fenjian.resource.DeliveryPlanQuery.dao.SortDeliveryDao;
import cac.fenjian.resource.ExcelExport.dao.ExcelExportDao;
import cac.fenjian.resource.ExcelExport.dto.DeliveryTrayDto;
import cac.fenjian.resource.ExcelExport.dto.SupportingDetailsDto;
import cac.fenjian.resource.ExcelExport.dto.WmsItemInventoryDto;
import cac.fenjian.resource.ExcelExport.service.ExcelExportService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

/**
 * @className: ExcelExportRest
 * @description: （描述这个类的作用）
 * @author: lqc
 * @date: 2023/10/19 11:03:03 周四
 * @version: 1.0
 */
@RestController
@RequestMapping("/exceltest")
public class ExcelExportRest {

    @Autowired
    private ExcelExportDao excelExportDao;
    @Autowired
    private SortDeliveryDao sortDeliveryDao;
    @Autowired
    private ExcelExportService excelExportService;

    @PostMapping("/excel")
    public void ExcelExportWrite(HttpServletResponse response,@RequestBody HashMap queryParam) {
        try {
            List<WmsItemInventoryDto> wmsItemInventoryDtos = excelExportDao.searchWmsItemInventory(queryParam);
            String filename = URLEncoder.encode(System.currentTimeMillis() + ".xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + filename);//设定输出文件头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");// 定义输出类型
            EasyExcel.write(response.getOutputStream(), WmsItemInventoryDto.class)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .autoCloseStream(Boolean.FALSE).sheet("台账")
                    .doWrite(wmsItemInventoryDtos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response,@RequestBody HashMap queryParam) {
        try {
            String filename = URLEncoder.encode(System.currentTimeMillis() + ".xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + filename);//设定输出文件头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");// 定义输出类型
            if(Integer.valueOf(String.valueOf(queryParam.get("radio")))==2){
                //从数据库查询需写入excel的结果集
                List<DeliveryTrayDto> hashMaps = excelExportDao.querySendTool(queryParam);
                //配送托盘
                //DeliveryTrayDto.class对应实体
                EasyExcel.write(response.getOutputStream(), DeliveryTrayDto.class)
                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                        .autoCloseStream(Boolean.FALSE).sheet("配送托盘")
                        .doWrite(hashMaps);
            }else{
                List<SupportingDetailsDto> hashMaps = excelExportDao.querySendMaterial(queryParam);
                //配套明细
                EasyExcel.write(response.getOutputStream(), SupportingDetailsDto.class)
                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                        .autoCloseStream(Boolean.FALSE).sheet("配套明细")
                        .doWrite(hashMaps);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败！");
        }
    }

}
